/******************************************************************************

    Copyright (C) 2006-2010 Alessio Stalla (alessiostalla@gmail.com)

    This file is part of PrettyProlog.

    PrettyProlog is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    PrettyProlog is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrettyProlog; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    See the file COPYING for further details.

 *****************************************************************************/

package prettyprolog.engine.syspreds;

import java.util.List;

import prettyprolog.datatypes.CharString;
import prettyprolog.datatypes.Constant;
import prettyprolog.datatypes.Substitution;
import prettyprolog.datatypes.Symbol;
import prettyprolog.datatypes.Term;
import prettyprolog.engine.Engine;

/**
 * Implements the system predicate infix/2, that registers a symbol as infix.
 * The system predicate takes 2 arguments: a string or symbol, naming the symbol
 * to be declared infix; and another Term <code>t</code> that, when equal to the
 * constant <code>true</code>, makes the infix symbol be <i>terminating</i>
 * (meaning that the symbol's name cannot be part of the name of another
 * symbol). When <code>t</code> is not <code>true</code>, the symbol is
 * nonterminating. <br />
 * So for example '+' should be terminating (because 'a+b' is to be parsed as 3
 * distinct symbols, a, + and b, and not as the single symbol a+b), while 'is'
 * should be not, because 'fish' is to be parsed as a single symbol and not as
 * the sequence s, is, h.
 * 
 */
public class Infix extends SystemPredicate {

    private static final Infix inst = new Infix();

    private Infix() {
        super(2);
    }

    /** Returns the only instance of this system predicate. */
    public static Infix getInstance() {
        return inst;
    }

    /**
     * @throws SyspredException
     *             if the argument is not a clause.
     */
    @Override
    protected SyspredEffect doCall(Engine en, Substitution subst, List<Term> args) throws SyspredException {
        Symbol s;
        if (args.get(0) instanceof CharString) {
            s = prettyprolog.datatypes.Namespace.current().internInfix(((CharString) args.get(0)).getString());
        } else if (args.get(0) instanceof Constant) {
            s = ((Constant) args.get(0)).getSymbol();
            s.setInfix(true);
        } else {
            throw new SyspredException("infix/2 requires a constant, not " + args.get(0));
        }
        if (args.get(1) instanceof Constant && ((Constant) args.get(1)).getSymbol().getName().equals("true")) {
            s.setTerminating(true);
        }
        return new SyspredEffect(false, subst);
    }

    public boolean isBacktrackable() {
        return false;
    }

    public String toString() {
        return super.toString() + " infix/2";
    }
}
